<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>班级管理</title>
    <style>
        .action-buttons .btn {
            margin-right: 5px;
        }
        .modal-header {
            background-color: #007bff;
            color: white;
        }
        .table-actions {
            width: 150px;
            text-align: center;
        }
    </style>
</head>
<body>
<jsp:include page="layout/header.jsp"/>
<div class="container mt-4">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">班级管理</h5>
                    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#addClazzModal">
                        <i class="fas fa-plus"></i> 新增班级
                    </button>
                </div>
                <div class="card-body">
                    <!-- 搜索表单 -->
                    <form class="form-inline mb-4" action="${pageContext.request.contextPath}/clazz/list.do" method="get">
                        <div class="input-group">
                            <input class="form-control" type="text" name="name" placeholder="搜索班级名称" value="${name}"/>
                            <div class="input-group-append">
                                <button class="btn btn-primary" type="submit">
                                    <i class="fas fa-search"></i> 查询
                                </button>
                                <a class="btn btn-secondary" href="${pageContext.request.contextPath}/clazz/list.do">
                                    <i class="fas fa-times"></i> 清空
                                </a>
                            </div>
                        </div>
                    </form>
                    
                    <!-- 错误提示 -->
                    <c:if test="${not empty error}">
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            ${error}
                            <button type="button" class="close" data-dismiss="alert">
                                <span>&times;</span>
                            </button>
                        </div>
                    </c:if>
                    
                    <!-- 数据表格 -->
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="thead-light">
                                <tr>
                                    <th>ID</th>
                                    <th>班级名称</th>
                                    <th>教室</th>
                                    <th>开课时间</th>
                                    <th>结课时间</th>
                                    <th>班主任</th>
                                    <th>状态</th>
                                    <th class="table-actions">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <c:forEach var="c1" items="${list}">
                                    <tr>
                                        <td>${c1.id}</td>
                                        <td>${c1.name}</td>
                                        <td>${c1.room}</td>
                                        <td>${c1.beginDate}</td>
                                        <td>${c1.endDate}</td>
                                        <td>${c1.masterName}</td>
                                        <td>${c1.status}</td>
                                        <td class="action-buttons">
                                            <button type="button" class="btn btn-sm btn-primary" 
                                                    onclick="editClazz(${c1.id}, '${c1.name}', '${c1.room}', '${c1.beginDate}', '${c1.endDate}', ${c1.masterId}, ${c1.subject})" 
                                                    data-toggle="modal" data-target="#editClazzModal">
                                                <i class="fas fa-edit"></i> 编辑
                                            </button>
                                            <button type="button" class="btn btn-sm btn-danger" 
                                                    onclick="deleteClazz(${c1.id}, '${c1.name}')">
                                                <i class="fas fa-trash"></i> 删除
                                            </button>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="mt-3">
                        <a class="btn btn-secondary" href="../pages/main.jsp">
                            <i class="fas fa-arrow-left"></i> 返回首页
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 新增班级弹窗 -->
<div class="modal fade" id="addClazzModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">新增班级</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="${pageContext.request.contextPath}/clazz/add.do" method="post">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addClazzName">班级名称 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="addClazzName" name="name" 
                                       placeholder="请输入班级名称" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addClazzRoom">教室</label>
                                <input type="text" class="form-control" id="addClazzRoom" name="room" 
                                       placeholder="请输入教室">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addClazzBeginDate">开课时间</label>
                                <input type="date" class="form-control" id="addClazzBeginDate" name="beginDateStr">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addClazzEndDate">结课时间</label>
                                <input type="date" class="form-control" id="addClazzEndDate" name="endDateStr">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addClazzMaster">班主任</label>
                                <select class="form-control" id="addClazzMaster" name="masterId">
                                    <option value="">请选择班主任</option>
                                    <c:forEach var="emp" items="${empList}">
                                        <option value="${emp.id}">${emp.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="addClazzSubject">科目</label>
                                <select class="form-control" id="addClazzSubject" name="subject">
                                    <option value="">请选择科目</option>
                                    <option value="1">Java开发</option>
                                    <option value="2">前端开发</option>
                                    <option value="3">Python开发</option>
                                    <option value="4">大数据</option>
                                    <option value="5">人工智能</option>
                                    <option value="6">UI设计</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-success">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑班级弹窗 -->
<div class="modal fade" id="editClazzModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">编辑班级</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <form action="${pageContext.request.contextPath}/clazz/update.do" method="post">
                <input type="hidden" id="editClazzId" name="id">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editClazzName">班级名称 <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" id="editClazzName" name="name" 
                                       placeholder="请输入班级名称" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editClazzRoom">教室</label>
                                <input type="text" class="form-control" id="editClazzRoom" name="room" 
                                       placeholder="请输入教室">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editClazzBeginDate">开课时间</label>
                                <input type="date" class="form-control" id="editClazzBeginDate" name="beginDateStr">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editClazzEndDate">结课时间</label>
                                <input type="date" class="form-control" id="editClazzEndDate" name="endDateStr">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editClazzMaster">班主任</label>
                                <select class="form-control" id="editClazzMaster" name="masterId">
                                    <option value="">请选择班主任</option>
                                    <c:forEach var="emp" items="${empList}">
                                        <option value="${emp.id}">${emp.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="editClazzSubject">科目</label>
                                <select class="form-control" id="editClazzSubject" name="subject">
                                    <option value="">请选择科目</option>
                                    <option value="1">Java开发</option>
                                    <option value="2">前端开发</option>
                                    <option value="3">Python开发</option>
                                    <option value="4">大数据</option>
                                    <option value="5">人工智能</option>
                                    <option value="6">UI设计</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<script>
    function editClazz(id, name, room, beginDate, endDate, masterId, subject) {
        document.getElementById('editClazzId').value = id;
        document.getElementById('editClazzName').value = name;
        document.getElementById('editClazzRoom').value = room || '';
        document.getElementById('editClazzBeginDate').value = beginDate || '';
        document.getElementById('editClazzEndDate').value = endDate || '';
        document.getElementById('editClazzMaster').value = masterId || '';
        document.getElementById('editClazzSubject').value = subject || '';
    }
    
    function deleteClazz(id, name) {
        if (confirm('确认删除班级 "' + name + '" 吗？\n注意：如果该班级下有学员，将无法删除。')) {
            window.location.href = 'delete.do?id=' + id;
        }
    }
</script>

<jsp:include page="layout/footer.jsp"/>
</body>
</html>

